import orderActions from 'actions/order'
// import userActions from 'actions/users'
import { saveData } from 'utils'
import { message } from 'antd'

export default {
  state: {
    orderDetail: null,
    childOrderList: []
  },
  reducers: {
    save (state, payload) {
      return saveData(state, payload)
    }
  },
  effects: dispatch => ({
    // 获取回收站列表
    async getOrderList (params) {
      this.save({
        orderData: null
      })
      const res = await orderActions.getOrderList(params)
      if (res.success) {
        this.save({
          orderData: {
            list: res.data.orderList,
            total: res.data.totalCount
          }
        })
      } else {
        message.error(res.msg)
      }
    },
    // 获取订单详情
    async getOrderDetail (params) {
      // this.save({
      //   orderDetail: null
      // })
      const res = await orderActions.getOrderDetails(params)
      if (res.success) {
        const { childOrderList = [] } = res.data
        this.save({
          orderDetail: res.data,
          childOrderList
        })
      } else {
        message.error(res.msg)
      }
    },
    // 备注
    async remarkOrder (params) {
      try {
        await orderActions.remarkOrder(params)
      } catch (error) {

      }
    },
    // 关闭订单
    async closeOrder (params) {
      try {
        await orderActions.closeOrder(params)
      } catch (error) {

      }
    },
    // app推送
    async sendMessage (params) {
      try {
        await orderActions.sendMessage(params)
      } catch (error) {

      }
    },
    // 物流信息
    async logistics (params) {
      try {
        this.save({
          logisticsList: null
        })
        const res = await orderActions.logistics(params)
        if (res.success) {
          this.save({
            logisticsList: res.data
          })
        }
      } catch (error) {

      }
    },
    // 订单基础设置
    async infrastructure (params) {
      try {
        await orderActions.infrastructure(params)
      } catch (error) {
        console.log('infrastructure -> error', error)
      }
    },
    async getInfrastructureDetail (params) {
      try {
        this.save({
          infrastructureDetail: {}
        })
        const res = await orderActions.infrastructureDetail(params)
        if (res.success) {
          this.save({
            infrastructureDetail: res.data
          })
        }
      } catch (error) {

      }
    },
    async changeInvoice (params) {
      try {
        await orderActions.changeInvoice(params)
      } catch (error) {

      }
    },
    // 修改收货人信息
    async changeReceiveInfo (params) {
      try {
        await orderActions.changeReceiveInfo(params)
      } catch (error) {

      }
    },
    saveChildOrderInfo (item, state) {
      const { order: { childOrderList } } = state
      const temp = childOrderList.map(n => {
        if (n.childOrderId === item.childOrderId) {
          n = {
            ...n,
            ...item
          }
        }
        return n
      })
      this.save({
        childOrderList: temp
      })
    },
    async changeOrderCommodity (params, state) {
      try {
        const { order: { childOrderList, orderDetail } } = state
        const { orderId } = orderDetail
        const temp = childOrderList.map(n => {
          const { childOrderId, commodityName, commodityPrice, commoditySpec, ramark } = n
          return {
            childOrderId,
            commodityName,
            commodityPrice,
            commoditySpec,
            ramark
          }
        })
        await orderActions.changeOrderCommodity({
          orderId,
          commodityInfoNodes: temp
        })
      } catch (error) {

      }
    },
    async changeOrderCostInfo (params) {
      try {
        await orderActions.changeOrderCostInfo(params)
      } catch (error) {

      }
    },
    async orderDeliveredInfo (params) {
      try {
        this.save({
          deliveredInfo: {}
        })
        const res = await orderActions.orderDeliveredInfo(params)
        if (res.success) {
          this.save({
            deliveredInfo: res.data
          })
        }
      } catch (error) {

      }
    },
    async getLogisticsInfo (params) {
      try {
        this.save({
          logisticsList: []
        })
        const res = await orderActions.getLogisticsInfo(params)
        if (res.success) {
          this.save({
            logisticsList: res.data
          })
        }
      } catch (error) {

      }
    },
    async refund (params) {
      try {
        await orderActions.refund(params)
      } catch (error) {

      }
    },
    async refuse (params) {
      await orderActions.refuse(params)
    },
    async addCustomOrder (params) {
      try {
        await orderActions.addCustomOrder(params)
      } catch (error) {

      }
    },
    async getCategoryTree (params) {
      this.save({
        categoryTreeOptions: []
      })
      try {
        const res = await orderActions.getCategoryTree(params)
        if (res.success) {
          this.save({
            categoryTreeOptions: res.data
          })
        }
      } catch (error) {

      }
    },
    async getAddressList (params) {
      try {
        this.save({
          addressList: []
        })
        const res = await orderActions.getAddressList(params)
        if (res.success) {
          this.save({
            addressList: res.data
          })
        }
      } catch (error) {

      }
    },
    // 订单-部分发货
    async orderDelivery (params) {
      try {
        await orderActions.orderDelivery(params)
      } catch (error) {

      }
    },
    // 对账-列表
    async getOrderAccountList (params) {
      try {
        this.save({
          accountData: {}
        })
        const res = await orderActions.getOrderAccountList(params)
        if (res.success) {
          this.save({
            accountData: res.data
          })
        }
      } catch (error) {

      }
    },
    // 批量对账
    async checkAccount (params) {
      await orderActions.checkAccount(params)
    },
    async reversalAccount (params) {
      await orderActions.reversalAccount(params)
    },
    async updateReconciliation (params) {
      await orderActions.updateReconciliation(params)
    },
    // 退货款单详情
    async getRefundDetailInfo (params) {
      this.save({
        refundDetailInfo: {}
      })
      const res = await orderActions.getRefundDetailInfo(params)
      if (res.success) {
        this.save({
          refundDetailInfo: res.data
        })
      }
    }
  })
}
